package com.jyy.prefabricated.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.Expert;
import com.jyy.prefabricated.service.ExpertService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 专家管理
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class ExpertController {

    @Autowired
    private ExpertService expertService;

    /**
     * 新增专家
     * @param expert
     * @return
     */
    @PostMapping("/expert")
    public AjaxResult insert(@Valid @RequestBody Expert expert){
        log.info("Rest request to insert expert:{}",expert);
        Integer id = expertService.insert(expert);
        Expert result = expertService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新专家
     * @param expert
     * @return
     */
    @PutMapping("/expert")
    public AjaxResult update(@Valid @RequestBody Expert expert){
        log.info("Rest request to update expert:{}",expert);
        expertService.update(expert);
        Expert result = expertService.selectById(expert.getId());
        return AjaxResult.success(result);
    }

    /**
     * 批量更新专家状态
     * @param dto
     * @return
     */
    @PutMapping("/expert-batch")
    public AjaxResult update(@Valid @RequestBody BatchDto dto){
        log.info("Rest request to batch-update expert:{}",dto);
        expertService.batchUpdateStatus(dto);
        return AjaxResult.success();
    }

    /**
     * 批量删除专家
     * @param dto
     * @return
     */
    @DeleteMapping("/expert-batch")
    public AjaxResult deleteById(@RequestBody BatchDto dto){
        log.info("Rest request to batch-delete expert by id:{}",dto);
        int row = expertService.batchDelete(dto);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 更新专家状态
     * @param id
     * @return
     */
    @PutMapping("/expert-status/{id}")
    public AjaxResult updateStatus(@PathVariable Integer id){
        log.info("Rest request to update user status by id:{}",id);
        int row = expertService.updateStatus(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询专家详情
     * @param id
     * @return
     */
    @GetMapping("/expert/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find expert by id:{}",id);
        Expert result = expertService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除专家
     * @param id
     * @return
     */
    @DeleteMapping("/expert/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete expert by id:{}",id);
        int row = expertService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 分页查询专家列表
     * @param name 名称
     * @param status 状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/expert")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status, Pageable pageable){
        log.info("Rest request to find expert by params name:{},status:{}",name, status);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Expert> experts = expertService.selectByParams(name, status);
        PageInfo<Expert> result = new PageInfo<>(experts);
        return AjaxResult.success(result);
    }

    /**
     * 查询专家列表
     * @param name 名称
     * @param status 状态
     * @return
     */
    @GetMapping("/expert-list")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String name, @RequestParam(required = false) Integer status){
        log.info("Rest request to find expert by params name:{},status:{}",name, status);
        List<Expert> result = expertService.selectByParams(name, status);
        return AjaxResult.success(result);
    }
}
